//
//  ViewController.m
//  Ratings
//
//  Created by steven.zhan on 14-9-15.
//  Copyright (c) 2014年 hpxbzz. All rights reserved.
//

#import "GesturesViewController.h"

@interface GesturesViewController ()

@end

@implementation GesturesViewController

@synthesize players;
@synthesize delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"BestPlayers"]) {
        UINavigationController *navigation = segue.destinationViewController;
        RankingViewController *rankingViewControoler = [navigation.viewControllers objectAtIndex:0];
        rankingViewControoler.rankedPlayers=[self playersWithRating:5];
        rankingViewControoler.requiredRating=5;
        rankingViewControoler.delegate=self;
        rankingViewControoler.title=@"Best Player";
    }
    else if ([segue.identifier isEqualToString:@"WorstPlayers"]) {
        UINavigationController *navigation = segue.destinationViewController;
        RankingViewController *rankingViewControoler = [navigation.viewControllers objectAtIndex:0];
        rankingViewControoler.rankedPlayers=[self playersWithRating:1];
        rankingViewControoler.requiredRating=1;
        rankingViewControoler.delegate=self;
        rankingViewControoler.title=@"Worst Player";
    }
}

- (NSMutableArray*)playersWithRating:(int)rating
{
    NSMutableArray *rankedPlayers = [NSMutableArray arrayWithCapacity:self.players.count];
    for (Player *player in self.players) {
        if (player.rating==rating) {
            [rankedPlayers addObject:player];
        }
    }
    return rankedPlayers;
}

#pragma mark - RankingViewControllerDelegate

- (void)rankingViewControllerDidChangeRate:(RankingViewController*)viewController
                         rateChangedPlayer:(Player*)player
{
    [self.delegate gesturesViewControllerPlayerChange:self changedPlayer:player];
}

@end
